package org.PropertyManageMentSystem.Manager;

import org.PropertyManageMentSystem.Data.Complaint;
import org.PropertyManageMentSystem.Data.ComplaintReply;
import org.PropertyManageMentSystem.Data.MysqlConfig;

import java.sql.*;
import java.util.ArrayList;

public class ComplaintReplyManager {
    private Connection conn;

    public ComplaintReplyManager(MysqlConfig config) {
        String url = config.getUrl();
        String user = config.getUser();
        String password = config.getPassword();

        try {
            //加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        try {
            //创建连接
            this.conn = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void addEmptyComplaintReply(Long complaintId, ComplaintReply.Role role, Long id) {
        try {
            String sql = "Insert into complaintreply (complaintId, role, id, state) values(?, ?, ?, ?)";
            PreparedStatement stat = conn.prepareStatement(sql);//SQL语句已预编译并存储在PreparedStatement对象中。

            stat.setLong(1, complaintId); //即将p.getName()所获得值赋给第1个问号的地方
            String roleStr = role.name();
            stat.setString(2, roleStr);
            stat.setLong(3, id);
            stat.setInt(4, ComplaintReply.ComplaintReplyState.unfinished.ordinal());

            //执行sql语句
            stat.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public ArrayList<ComplaintReply> getUnfilledReply(ComplaintReply.Role role, Long id) {
        ArrayList<ComplaintReply> replies = new ArrayList<ComplaintReply>();
        try {
            String sql = "select * from complaintreply where role=? and id=? and state=?";
            PreparedStatement stat = conn.prepareStatement(sql);

            String roleStr = role.name();
            stat.setString(1, roleStr);
            stat.setLong(2, id);
            stat.setInt(3, ComplaintReply.ComplaintReplyState.unfinished.ordinal());


            //因为executeQuery会有ResultSet的返回值
            ResultSet res = stat.executeQuery();

            //将res中的值取出给到集合里面
            while (res.next()) {
                Long complaintReplyId = res.getLong("complaintReplyId");
                Long complaintId = res.getLong("complaintId");
                String info = res.getString("info");
                int stateNum = res.getInt("state");

                ComplaintReply.ComplaintReplyState state = ComplaintReply.ComplaintReplyState.values()[stateNum];
                ComplaintReply reply = new ComplaintReply(complaintReplyId, complaintId, role, id, info, state);
                //添加到容器
                replies.add(reply);
            }
            return replies;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void fillComplaintReply(Long complaintReplyId, String info) {
        try {
            String sql = "update complaintreply set info=?,state=? where complaintReplyId=?";
            PreparedStatement stat = conn.prepareStatement(sql);

            stat.setString(1, info);
            stat.setInt(2, ComplaintReply.ComplaintReplyState.finished.ordinal());
            stat.setLong(3, complaintReplyId);
            stat.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public ArrayList<ComplaintReply> getComplaintReplyByComplaintId(Long complaintId){
        ArrayList<ComplaintReply> replies = new ArrayList<ComplaintReply>();
        try {
            String sql = "select * from complaintreply where complaintId=?";
            PreparedStatement stat = conn.prepareStatement(sql);

            stat.setLong(1, complaintId);


            //因为executeQuery会有ResultSet的返回值
            ResultSet res = stat.executeQuery();

            //将res中的值取出给到集合里面
            while (res.next()) {
                Long complaintReplyId = res.getLong("complaintReplyId");
                String info = res.getString("info");
                ComplaintReply.Role role = ComplaintReply.Role.valueOf(res.getString("role"));
                Long id = res.getLong("id");
                int stateNum = res.getInt("state");
                ComplaintReply.ComplaintReplyState state = ComplaintReply.ComplaintReplyState.values()[stateNum];
                ComplaintReply reply = new ComplaintReply(complaintReplyId, complaintId, role, id, info, state);
                //添加到容器
                replies.add(reply);
            }
            return replies;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
